package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.domain.ViewpointRefComment;
import cn.wolfcode.luowowo.comment.repository.ViewpointRefCommentMongoRepository;
import cn.wolfcode.luowowo.comment.service.IViewpointRefCommentService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class ViewpointRefCommentServiceImpl implements IViewpointRefCommentService {
    @Autowired
    private ViewpointRefCommentMongoRepository repository;

    @Override
    public ViewpointRefComment commentRefAdd(ViewpointRefComment comment) {
        //填充评论时间
        comment.setCreateTime(new Date());
        //区别普通评论和回复评论
        if(comment.getType() == ViewpointRefComment.VIEWPOINT_COMMENT_TYPE){//回复性评论
            //根据id查询到原评论
            ViewpointRefComment refcomment = repository.findById(comment.getRefComment().getId()).get();
            //设置当前评论的引用
            comment.setRefComment(refcomment);
        }
        repository.save(comment);
        return comment;
    }

    //通过评论id查所有回复评论
    public List<ViewpointRefComment> listRefCommentsByCommentId(Long commentId) {
        return repository.findByViewpointCommentId(commentId);
    }


}
